<?php

include_once '../include/core/image.php';
include_once '../include/core/Excel/excel_reader2.php';
include_once dirname(dirname(__FILE__)) . '/include/common.php';
if ($_SERVER["CONTENT_LENGTH"] / (1024 * 1024) < 8) {
    $core = new core();
    $temp_name = $_FILES['file']['tmp_name'];
    $img_parts = pathinfo($_FILES['file']['name']);
    $ext = strtolower($img_parts['extension']);

    $allowed_ext = array('xls', 'csv');
    if (!in_array($ext, $allowed_ext)) {
        echo '<p class="uperror">Invalid file!</p>';
        exit;
    }
    $temp_file_name = time() . rand(10000, 99999) . session_id() . '.' . $ext;
    $today = getdate();
    $folder_temp = $today["mday"] . "_" . $today["mon"] . "_" . $today["year"];
    if (!is_dir(dirname(dirname(__FILE__)) . "/tmp_photo/" . $folder_temp)) {
        mkdir(dirname(dirname(__FILE__)) . "/tmp_photo/" . $folder_temp);
    }
    $temp_path = dirname(dirname(__FILE__)) . "/tmp_photo/" . $folder_temp . "/" . $temp_file_name;
    try {
        if ($_FILES['file']['size'] > 8 * 1024 * 1024) {
            echo '<p class="uperror">Your file exceeds the size limit. Please make sure it is not larger than 8MB</p>';
            exit;
        }
        if (!move_uploaded_file($temp_name, $temp_path)) {
            echo '<p class="uperror">Upload file is failed.</p>';
            exit;
        }
        $face_name = "";
        $name = "";
        $face_link = "";
        $phone = '';
        $extra_phone = '';
        $address = '';
        $bank_account='';
        $province = '';
        $size = '';
        $vip = '';
       
        if ($ext == "xls") {
            $data = new Spreadsheet_Excel_Reader($temp_path, true, "UTF-8"); // khoi tao doi tuong doc file excel
            $rowsnum = $data->rowcount($sheet_index = 0); // lay so hang cua sheet
            $colsnum = $data->colcount($sheet_index = 0); // lay so cot cua sheet

            error_reporting(E_ALL ^ E_NOTICE);
            //validate error
            for ($i = 2; $i <= $rowsnum; $i++) {
                for ($j = 1; $j <= $colsnum; $j++) {
                    if ($j == 1) {
                        $face_name = trim($data->val($i, $j));
                    } elseif ($j == 2) {
                        $face_link = trim($data->val($i, $j));
                    } elseif ($j == 3) {
                        $name = trim($data->val($i, $j));
                    } elseif ($j == 4) {
                        $phone = trim($data->val($i, $j));
                    } elseif ($j == 5) {
                        $extra_phone = trim($data->val($i, $j));
                    } elseif ($j == 6) {
                        $address = trim($data->val($i, $j));
                    } elseif ($j == 7) {
                        $size = trim($data->val($i, $j));
                    }  elseif ($j == 8) {
                        $bank_account = trim($data->val($i, $j));
                    } elseif ($j == 9) {
                        $province = trim($data->val($i, $j));
                    } elseif ($j == 10) {
                        $vip = trim($data->val($i, $j));
                    }  else {
                        break;
                    }
                }

                if ($phone === "") {
                    echo '<p class="uperror">Line ' . $i . ': Please enter customer phone! Please correct it and upload the file again.</p>';
                    exit;
                }
                
            }
            $img_processor = new OSC_Image ();
            //insert DB
            for ($i = 2; $i <= $rowsnum; $i++) {
                for ($j = 1; $j <= $colsnum; $j++) {
                     if ($j == 1) {
                        $face_name = addslashes(trim($data->val($i, $j)));
                    } elseif ($j == 2) {
                        $face_link = addslashes(trim($data->val($i, $j)));
                    } elseif ($j == 3) {
                        $name = addslashes(trim($data->val($i, $j)));
                    } elseif ($j == 4) {
                        $phone = addslashes(trim($data->val($i, $j)));
                    } elseif ($j == 5) {
                        $extra_phone = addslashes(trim($data->val($i, $j)));
                    } elseif ($j == 6) {
                        $address = addslashes(trim($data->val($i, $j)));
                    } elseif ($j == 7) {
                        $size = addslashes(trim($data->val($i, $j)));
                    }  elseif ($j == 8) {
                        $bank_account = addslashes(trim($data->val($i, $j)));
                    } elseif ($j == 9) {
                        $province = intval(trim($data->val($i, $j)));
                    } elseif ($j == 10) {
                        $vip = intval(trim($data->val($i, $j)));
                    }  else {
                        break;
                    }
                }
               $phone = "0".$phone;
               $extra_phone = "0".$extra_phone;
                $exist_customer = $core->checkExistCustomer($phone);

                if (!$exist_customer) {
                   // $des= htmlentities($des);
                  
                    $core->db->query("INSERT INTO zen_customer (name,facebook_name,facebook_link,
                        phone,extra_phone,address,clothes_size,province,VIP,bank_account) 
                        VALUES ('".$name."','".$face_name."','".$face_link."','".$phone."','".$extra_phone."',
                            '".$address."','".$size."','$province','$vip','".$bank_account."')");
                }
            }
        }
        echo '  <p class="msg">Customer has been added</p>';
        exit;
    } catch (Exception $e) {
        echo '<p class="uperror">' . $e->getMessage() . '</p>';
        exit;
    }
} else {
    echo '<p class="uperror">Your file exceeds the size limit. Please make sure it is not larger than 8MB</p>';
    exit;
}
?> 